package com.zheng.upms.rpc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.mapper.UpmsPermissionMapper;
import com.zheng.upms.dao.mapper.UpmsSystemMapper;
import com.zheng.upms.dao.mapper.UpmsUserPermissionMapper;
import com.zheng.upms.dao.mapper.UpmsUserSystemMapper;
import com.zheng.upms.dao.model.*;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.upms.rpc.api.UpmsPermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author likun
 * @since 2017-12-29
 */
@Service
@Transactional
@BaseService
public class UpmsPermissionServiceImpl extends BaseServiceImpl<UpmsPermissionMapper, UpmsPermission> implements UpmsPermissionService {
    private static Logger _log = LoggerFactory.getLogger(UpmsPermissionServiceImpl.class);

    @Autowired
    UpmsSystemMapper upmsSystemMapper;

    @Autowired
    UpmsApiService upmsApiService;

    @Autowired
    UpmsUserPermissionMapper upmsUserPermissionMapper;
    @Autowired
    UpmsUserSystemMapper upmsUserSystemMapper;

    private JSONArray getTreeByRoleIdTest(Integer roleId) {
        // 角色已有权限
        List<UpmsRolePermission> rolePermissions = upmsApiService.selectUpmsRolePermisstionByUpmsRoleId(roleId);

        JSONArray systems = new JSONArray();
        // 系统
        List<UpmsSystem> upmsSystems = upmsSystemMapper.selectList(new EntityWrapper<UpmsSystem>().eq("status", 1).orderBy("orders", true));
        for (UpmsSystem upmsSystem : upmsSystems) {
            JSONObject node = new JSONObject();
            node.put("id", upmsSystem.getSystemId());
            node.put("name", upmsSystem.getTitle());
            node.put("nocheck", true);
            node.put("open", true);
            systems.add(node);
        }
        if (systems.size() > 0) {
            for (Object system : systems) {
                List<UpmsPermission> upmsPermissions = super.baseMapper.selectList(new EntityWrapper<UpmsPermission>()
                        .eq("status", 1).eq("system_id", ((JSONObject) system).getInteger("id")).orderBy("orders", true));
                if (upmsPermissions.size() == 0) continue;
                // 目录
                JSONArray folders = new JSONArray();
                for (UpmsPermission upmsPermission : upmsPermissions) {
                    if (upmsPermission.getPid().intValue() != 0 || upmsPermission.getType() != 1) continue;
                    JSONObject node = new JSONObject();
                    node.put("id", upmsPermission.getPermissionId());
                    node.put("name", upmsPermission.getName());
                    node.put("open", true);
                    for (UpmsRolePermission rolePermission : rolePermissions) {
                        if (rolePermission.getPermissionId().intValue() == upmsPermission.getPermissionId().intValue()) {
                            node.put("checked", true);
                        }
                    }
                    folders.add(node);
                    // 菜单
                    JSONArray menus = new JSONArray();
                    for (Object folder : folders) {
                        for (UpmsPermission upmsPermission2 : upmsPermissions) {
                            if (upmsPermission2.getPid().intValue() != ((JSONObject) folder).getIntValue("id") || upmsPermission2.getType() != 2)
                                continue;
                            JSONObject node2 = new JSONObject();
                            node2.put("id", upmsPermission2.getPermissionId());
                            node2.put("name", upmsPermission2.getName());
                            node2.put("open", true);
                            for (UpmsRolePermission rolePermission : rolePermissions) {
                                if (rolePermission.getPermissionId().intValue() == upmsPermission2.getPermissionId().intValue()) {
                                    node2.put("checked", true);
                                }
                            }
                            menus.add(node2);
                            // 按钮
                            JSONArray buttons = new JSONArray();
                            for (Object menu : menus) {
                                for (UpmsPermission upmsPermission3 : upmsPermissions) {
                                    if (upmsPermission3.getPid().intValue() != ((JSONObject) menu).getIntValue("id") || upmsPermission3.getType() != 3)
                                        continue;
                                    JSONObject node3 = new JSONObject();
                                    node3.put("id", upmsPermission3.getPermissionId());
                                    node3.put("name", upmsPermission3.getName());
                                    node3.put("open", true);
                                    for (UpmsRolePermission rolePermission : rolePermissions) {
                                        if (rolePermission.getPermissionId().intValue() == upmsPermission3.getPermissionId().intValue()) {
                                            node3.put("checked", true);
                                        }
                                    }
                                    buttons.add(node3);
                                }
                                if (buttons.size() > 0) {
                                    ((JSONObject) menu).put("children", buttons);
                                    buttons = new JSONArray();
                                }
                            }
                        }
                        if (menus.size() > 0) {
                            ((JSONObject) folder).put("children", menus);
                            menus = new JSONArray();
                        }
                    }
                }
                if (folders.size() > 0) {
                    ((JSONObject) system).put("children", folders);
                }
            }
        }
        return systems;
    }

    @Override
    public JSONArray getTreeByRoleId(Integer roleId) {
        // 角色已有权限
        List<UpmsRolePermission> rolePermissions = upmsApiService.selectUpmsRolePermisstionByUpmsRoleId(roleId);
        List<UpmsRoleSystem> roleSystems = upmsApiService.selectUpmsRoleSystemByUpmsRoleId(roleId);
        JSONArray nodeArray = new JSONArray();
        //系统信息
        List<UpmsSystem> upmsSystems = upmsSystemMapper.selectList(new EntityWrapper<UpmsSystem>().eq("status", 1).
                orderBy("orders", true));
        List<Integer> systemIds = new ArrayList<>();
        for (UpmsSystem upmsSystem : upmsSystems) {
            JSONObject node = new JSONObject();
            node.put("id", "system" + upmsSystem.getSystemId());
            node.put("realId", upmsSystem.getSystemId());
            node.put("isSystem", true);
            systemIds.add(upmsSystem.getSystemId());
            node.put("name", upmsSystem.getTitle());
            node.put("open", true);
            roleSystems.forEach(roleSystem -> {
                if (roleSystem.getSystemId().equals(upmsSystem.getSystemId())) {
                    node.put("checked", true);
                }
            });
            nodeArray.add(node);
        }
        List<UpmsPermission> upmsPermissions = super.baseMapper.selectList(new EntityWrapper<UpmsPermission>()
                .eq("status", 1).orderBy("orders", true).in("system_id", systemIds));
        if (nodeArray.size() > 0) {
            for (UpmsPermission permission : upmsPermissions) {
                JSONObject node = new JSONObject();
                node.put("id", "permission" + permission.getPermissionId());
                node.put("realId", permission.getPermissionId());
                node.put("name", permission.getName());
                node.put("isSystem", false);
                node.put("open", true);
                if (permission.getPid() == 0) {
                    node.put("pId", "system" + permission.getSystemId());
                } else {
                    node.put("pId", "permission" + permission.getPid());
                }
                rolePermissions.forEach(rolePermission -> {
                    //todo
                    if (rolePermission.getPermissionId().equals(permission.getPermissionId())) {
                        node.put("checked", true);
                    }
                });

                nodeArray.add(node);
            }
        }
        return nodeArray;

    }

    @Override
    public JSONArray getTreeByUserId(Integer userId, Byte type) {
        // 角色权限
        List<UpmsUserPermission> userPermissions = upmsUserPermissionMapper.selectList(new EntityWrapper<UpmsUserPermission>()
                .eq("user_id", userId).eq("type", type));
        List<UpmsUserSystem> userSystems = upmsUserSystemMapper.selectList(new EntityWrapper<UpmsUserSystem>().
                eq("user_id", userId).eq("type", type));
        JSONArray nodeArray = new JSONArray();
        //系统信息
        List<UpmsSystem> upmsSystems = upmsSystemMapper.selectList(new EntityWrapper<UpmsSystem>().eq("status", 1).
                orderBy("orders", true));
        List<Integer> systemIds = new ArrayList<>();
        upmsSystems.forEach(system -> {
            JSONObject node = new JSONObject();
            node.put("id", "system" + system.getSystemId());
            node.put("realId", system.getSystemId());
            node.put("isSystem", true);
            systemIds.add(system.getSystemId());
            node.put("name", system.getTitle());
            node.put("open", true);
            userSystems.forEach(userSystem -> {
                if (userSystem.getSystemId().equals(system.getSystemId())) {
                    node.put("checked", true);
                }
            });
            nodeArray.add(node);
        });
        List<UpmsPermission> upmsPermissions = super.baseMapper.selectList(new EntityWrapper<UpmsPermission>()
                .eq("status", 1).orderBy("orders", true).in("system_id", systemIds));
        if (nodeArray.size() > 0) {
            for (UpmsPermission permission : upmsPermissions) {
                JSONObject node = new JSONObject();
                node.put("id", "permission" + permission.getPermissionId());
                node.put("realId", permission.getPermissionId());
                node.put("name", permission.getName());
                node.put("isSystem", false);
                node.put("open", true);
                if (permission.getPid() == 0) {
                    node.put("pId", "system" + permission.getSystemId());
                } else {
                    node.put("pId", "permission" + permission.getPid());
                }
                userPermissions.forEach(userPermission -> {
                    if (userPermission.getPermissionId().equals(permission.getPermissionId())) {
                        node.put("checked", true);
                    }
                });
                nodeArray.add(node);
            }
        }
        return nodeArray;
    }
}
